package com.ruoyi.wms.service;

import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.domain.bo.partolStore.MaintenanceRecordsBo;
import com.ruoyi.wms.domain.vo.MaintenanceRecordsVo;
import com.ruoyi.wms.domain.entity.partolStore.MaintenanceRecords;
import com.ruoyi.wms.mapper.MaintenanceRecordsMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 保养记录Service业务层处理
 *
 * @author xhtr
 * @date 2024-12-04
 */
@RequiredArgsConstructor
@Service
public class MaintenanceRecordsService {

    private final MaintenanceRecordsMapper maintenanceRecordsMapper;

    /**
     * 查询保养记录
     */
    public MaintenanceRecordsVo queryById(Long id){
        return maintenanceRecordsMapper.selectVoById(id);
    }

    /**
     * 查询保养记录列表
     */
    public TableDataInfo<MaintenanceRecordsVo> queryPageList(MaintenanceRecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaintenanceRecords> lqw = buildQueryWrapper(bo);
        Page<MaintenanceRecordsVo> result = maintenanceRecordsMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询保养记录列表
     */
    public List<MaintenanceRecordsVo> queryList(MaintenanceRecordsBo bo) {
        LambdaQueryWrapper<MaintenanceRecords> lqw = buildQueryWrapper(bo);
        return maintenanceRecordsMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaintenanceRecords> buildQueryWrapper(MaintenanceRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaintenanceRecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEquipentId() != null, MaintenanceRecords::getEquipentId, bo.getEquipentId());
        lqw.between(params.get("beginMaintenanceTime") != null && params.get("endMaintenanceTime") != null,
            MaintenanceRecords::getMaintenanceTime ,params.get("beginMaintenanceTime"), params.get("endMaintenanceTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getMaintainer()), MaintenanceRecords::getMaintainer, bo.getMaintainer());
        return lqw;
    }

    /**
     * 新增保养记录
     */
    public void insertByBo(MaintenanceRecordsBo bo) {
        MaintenanceRecords add = MapstructUtils.convert(bo, MaintenanceRecords.class);
        maintenanceRecordsMapper.insert(add);
    }

    /**
     * 修改保养记录
     */
    public void updateByBo(MaintenanceRecordsBo bo) {
        MaintenanceRecords update = MapstructUtils.convert(bo, MaintenanceRecords.class);
        maintenanceRecordsMapper.updateById(update);
    }

    /**
     * 批量删除保养记录
     */
    public void deleteByIds(Collection<Long> ids) {
        maintenanceRecordsMapper.deleteBatchIds(ids);
    }
}
